package com.econage.eccm.test.base.webdatasource.facade;

import com.econage.eccm.test.base.webdatasource.WebDsExceptionUtil;
import com.google.common.base.Joiner;
import com.google.common.reflect.AbstractInvocationHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;

public abstract class JdbcFacade extends AbstractInvocationHandler {

    protected boolean compare(String name1, String name2) {
        return name1.equals(name2);
    }
    protected boolean compare(String methodName, Method method) {
        return compare(methodName, method.getName());
    }
    protected boolean process(String[] names, Method method) {
        boolean process = false;
        final String name = method.getName();
        for (int i=0; (!process) && i<names.length; i++) {
            process = compare(names[i],name);
        }
        return process;
    }

    protected final Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
        try{
            return doInvoke(proxy,method,args);
        }catch(Throwable t){
            throw WebDsExceptionUtil.unwrapThrowable(t);
        }
    }

    protected abstract Object doInvoke(Object proxy, Method method, Object[] args) throws Throwable;
}
